package cc.rengu.redp.bizimpl.utils;

/**
 * @Author: 刘玉亮
 * @CreateDate: 2021/6/10 0010 16:12
 * @UpdateDate: 2021/6/10 0010 16:12
 * @Version: 1.0
 * @Description:
 */

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DateUtils {
    private static final Logger log = LoggerFactory.getLogger(DateUtils.class);
    public static final String DEFAULT_TIMEZONE = "GMT+8";
    public static final String ISO_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
    public static final String ISO_SHORT_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
    public static final String DEFAULT_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    public static final String SHORT_TIME_FORMAT = "yyyy-MM-dd HH:mm";
    public static final String FULL_SEQ_FORMAT = "yyyyMMddHHmmssSSS";
    protected static final String[] MULTI_FORMAT = new String[]{"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss.SSS", "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM"};
    public static final String FORMAT_YYYY = "yyyy";
    public static final String FORMAT_YYYYMM = "yyyyMM";
    public static final String FORMAT_YYYYMMDD = "yyyyMMdd";
    public static final String FORMAT_YYYYMMDDHH = "yyyyMMddHH";
    private static Date currentDate;

    public DateUtils() {
    }

    public static String formatDate(Date date) {
        return date == null ? "" : (new SimpleDateFormat("yyyy-MM-dd")).format(date);
    }

    public static String formatDate(Date date, String format) {
        return date == null ? null : (new SimpleDateFormat(format)).format(date);
    }

    public static Integer formatDateToInt(Date date, String format) {
        return date == null ? null : Integer.valueOf((new SimpleDateFormat(format)).format(date));
    }

    public static Long formatDateToLong(Date date, String format) {
        return date == null ? null : Long.valueOf((new SimpleDateFormat(format)).format(date));
    }

    public static String formatTime(Date date) {
        return date == null ? null : (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(date);
    }

    public static String formatShortTime(Date date) {
        return date == null ? null : (new SimpleDateFormat("yyyy-MM-dd HH:mm")).format(date);
    }

    public static String formatDateNow() {
        return formatDate(currentDate());
    }

    public static String formatTimeNow() {
        return formatTime(currentDate());
    }

    public static Date parseDate(String date, String format) {
        if (date == null) {
            log.warn("parseDate error date is null to now date!");
            return new Date();
        } else {
            try {
                return (new SimpleDateFormat(format)).parse(date);
            } catch (ParseException var3) {
                throw new RuntimeException(var3);
            }
        }
    }

    public static Date parseTime(String date, String format) {
        if (date == null) {
            return null;
        } else {
            try {
                return (new SimpleDateFormat(format)).parse(date);
            } catch (ParseException var3) {
                throw new RuntimeException(var3);
            }
        }
    }

    public static Date parseDate(String date) {
        return parseDate(date, "yyyy-MM-dd");
    }

    public static Date parseTime(String date) {
        return parseTime(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String plusOneDay(String date) {
        DateTime dateTime = new DateTime(parseDate(date).getTime());
        return formatDate(dateTime.plusDays(1).toDate());
    }

    public static String plusOneDay(Date date) {
        DateTime dateTime = new DateTime(date.getTime());
        return formatDate(dateTime.plusDays(1).toDate());
    }

    public static String getHumanDisplayForTimediff(Long diffMillis) {
        if (diffMillis == null) {
            return "";
        } else {
            long day = diffMillis / 86400000L;
            long hour = diffMillis / 3600000L - day * 24L;
            long min = diffMillis / 60000L - day * 24L * 60L - hour * 60L;
            long se = diffMillis / 1000L - day * 24L * 60L * 60L - hour * 60L * 60L - min * 60L;
            StringBuilder sb = new StringBuilder();
            if (day > 0L) {
                sb.append(day + "D");
            }

            DecimalFormat df = new DecimalFormat("00");
            sb.append(df.format(hour) + ":");
            sb.append(df.format(min) + ":");
            sb.append(df.format(se));
            return sb.toString();
        }
    }

    public static Date[] parseBetweenDates(String date) {
        if (StringUtils.isBlank(date)) {
            return null;
        } else {
            date = date.replace("～", "~");
            Date[] dates = new Date[2];
            String[] values = date.split("~");
            dates[0] = parseMultiFormatDate(values[0].trim());
            dates[1] = parseMultiFormatDate(values[1].trim());
            return dates;
        }
    }

    public static Date parseMultiFormatDate(String date) {
        try {
            return org.apache.commons.lang3.time.DateUtils.parseDate(date, MULTI_FORMAT);
        } catch (ParseException var2) {
            log.error("error = [{}]", var2);
            return null;
        }
    }

    public static Long getDiffDay(String beginDate, String endDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Long checkday = 0L;

        try {
            checkday = (formatter.parse(endDate).getTime() - formatter.parse(beginDate).getTime()) / 86400000L;
        } catch (ParseException var5) {
            log.error("error = [{}]", var5);
            checkday = null;
        }

        return checkday;
    }

    public static Long getDiffDay(Date beginDate, Date endDate) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strBeginDate = format.format(beginDate);
        String strEndDate = format.format(endDate);
        return getDiffDay(strBeginDate, strEndDate);
    }

    public static Date nDaysAfter(Integer n, Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(5, cal.get(5) + n);
        return cal.getTime();
    }

    public static Date nDaysAgo(Integer n, Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(5, cal.get(5) - n);
        return cal.getTime();
    }

    public static Date setCurrentDate(Date date) {
        if (date == null) {
            currentDate = null;
        } else {
            currentDate = date;
        }

        return currentDate;
    }

    public static Date currentDate() {
        return currentDate == null ? new Date() : currentDate;
    }

    public static Integer getWeekOfYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        String yearString = formatDate(date, "yyyy");
        int weekNum = c.get(3);
        if (weekNum < 10) {
            yearString = StringUtils.rightPad(yearString, 5, "0");
        }

        return Integer.valueOf(yearString + weekNum);
    }

    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        c.add(2, 0);
        c.set(5, 1);
        System.out.println(formatDate(c.getTime(), "yyyyMMdd"));
        formatDate(new Date(), "yyyy-MM-dd");
        Pattern p = Pattern.compile("(\\d{4})-(\\d{1,2})-(\\d{1,2})");
        Matcher m = p.matcher(formatDate(new Date(), "yyyy-MM-dd"));
        if (m.find()) {
            System.out.println("日期:" + m.group());
            System.out.println("年:" + m.group(1));
            System.out.println("月:" + m.group(2));
            System.out.println("日:" + m.group(3));
        }

        String time = "2015/11/02 ";
        log.info("date = {}", parseDate(time, "yyyy/MM/dd"));
    }
}
